/**
 * Dwr remote call  page reader
 * @param meta {Object} Page object elements
 * <pre>
 * id {String} Record primary key field
 * data {String} Records field
 * total {String} Record total number field
 * </pre>
 * @param recordType {Object} recordType Either an Array of field definition objects as specified
 */
CQCIS.data.DWRPageReader = function(meta, recordType) {
    var m = Ext.applyIf(meta ? meta : {}, {
        data: 'result',
        total: 'totalCount'
    });
    CQCIS.data.DWRPageReader.superclass.constructor.call(this, m, recordType);
};

Ext.extend(CQCIS.data.DWRPageReader, Ext.data.DataReader, {
    getJsonAccessor: function() {
        var re = /[\[\.]/;
        return function(expr) {
            try {
                return (re.test(expr)) ? new Function("obj", "return obj." + expr) : function(obj) {
                    return obj[expr];
                };
            } catch (e) {
            }
            return Ext.emptyFn;
        };
    }(),
    
    read: function(o) {
        var recordType = this.recordType, fields = recordType.prototype.fields;
        
        if (!this.ef) {
            if (this.meta.total) {
                this.getTotal = this.getJsonAccessor(this.meta.total);
            }
            if (this.meta.success) {
                this.getSuccess = this.getJsonAccessor(this.meta.success);
            }
            if (this.meta.data) {
                this.getData = this.getJsonAccessor(this.meta.data);
            } else {
                this.getData = function(o) {
                    return o.data;
                };
            }
            if (this.meta.id) {
                var g = this.getJsonAccessor(this.meta.id);
                this.getId = function(rec) {
                    var r = g(rec);
                    return (r === undefined || r === "") ? null : r;
                };
            } else {
                this.getId = function() {
                    return null;
                };
            }
            
            this.ef = [];
            for (var i = 0; i < fields.length; i++) {
                var f = fields.items[i];
                var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
                this.ef[i] = this.getJsonAccessor(map);
            }
        }
        
        
        var records = [];
        var root = this.getData(o), c = root.length, totalRecords = c, success = true;
        
        if (this.meta.total) {
            var v = parseInt(this.getTotal(o), 10);
            if (!isNaN(v)) {
                totalRecords = v;
            }
        }
        
        if (this.meta.success) {
            var w = this.getSuccess(o);
            if (w === false || w === 'false') {
                success = false;
            }
        }
        
        for (var j = 0; j < c; j++) {
            var n = root[j];
            var values = {};
            var id = this.getId(n);
            for (var k = 0; k < fields.length; k++) {
                f = fields.items[k];
                var z = this.ef[k](n);
                values[f.name] = f.convert((z !== undefined && z !== null) ? z : f.defaultValue);
            }
            var record = new recordType(values, id);
            records[j] = record;
        }
        
        return {
            success: success,
            records: records,
            totalRecords: totalRecords
        };
    }
});
